package com.bigdata.hdfs;

import cn.hutool.core.util.StrUtil;
import com.bigdata.commom.PropConstant;
import com.common.utils.PropertiesUtils;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;

/**
 * @author xin.ding
 * @date 2020/10/27 10:44
 */
public class Hdfs {
    private static String operation;
    private static String localPath;
    private static String hdfsPath;

    public static void main(String[] args) throws Exception {
        parseArgs(args);
        HdfsOper hdfsOper = new HdfsOper();
        switch (operation) {
            case "ls":
                hdfsOper.list(hdfsPath);
                break;
            case "cat":
                System.out.println(String.format("======cat %s ======", hdfsPath));
                System.out.println(hdfsOper.cat(hdfsPath));
                break;
            case "upload":
                hdfsOper.uploadFileToHdfs(localPath, hdfsPath);
                System.out.println(String.format("finish upload local file {%s} to hdfs {%s} ", localPath, hdfsPath));
                break;
            case "delete":
                hdfsOper.delete(hdfsPath);
                System.out.println("finish delete " + hdfsPath);
                break;
            case "download":
                hdfsOper.downloadFileFromHdfs(hdfsPath, localPath);
                System.out.println(String.format("save hdfs file {%s} to {%s}", hdfsPath, localPath));
                break;
            default:
                System.out.println("can't exec operation " + operation + ",only support [ls,cat,upload,delete]");
                break;
        }
    }

    private static void parseArgs(String[] args) throws Exception {
        CommandLineParser parser = new DefaultParser();
        org.apache.commons.cli.Options options = new Options();
        options.addOption("op", "operation", true, "operation");
        options.addOption("lp", "local-path", true, "local file path");
        options.addOption("hp", "hdfs-path", true, "hdfs file path");
        CommandLine commandLine = parser.parse(options, args, true);

        if (commandLine.hasOption("operation")) {
            operation = commandLine.getOptionValue("operation");
        }
        if (commandLine.hasOption("local-path")) {
            localPath = commandLine.getOptionValue("local-path");
        }
        if (commandLine.hasOption("hdfs-path")) {
            hdfsPath = commandLine.getOptionValue("hdfs-path");
        }
        if (StrUtil.isAllBlank(operation, hdfsPath)) {
            System.out.println("use java -jar bigdata-test.jar -op xx [-lp xx] -hp xx");
            System.exit(1);
        }
        System.out.println(String.format("args info:operation %s,local-path %s, hdfs-path %s", operation, localPath, hdfsPath));
    }

    public static void connect1() throws Exception {
        HdfsMain hdfsMain = new HdfsMain();
        hdfsMain.initConf(PropertiesUtils.getString(PropConstant.HDFS_CORE_PATH)
                , PropertiesUtils.getString(PropConstant.HDFS_SITE_PATH)
                , PropertiesUtils.getString(PropConstant.HDFS_HOME_PATH)
                , PropertiesUtils.getString(PropConstant.HDFS_USER));

        HdfsService hdfsService = new HdfsService(hdfsMain.getConfig());
//        hdfsService.mkdir("/conn");
        hdfsService.uploadFileToHdfs(localPath, hdfsPath);
    }
}
